<?php
/**
 * (actions)
 */
namespace App\Admin\Actions\Stocking;

use Encore\Admin\Actions\BatchAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Database\Eloquent\Collection;
class SkuBatchApply extends BatchAction
{
    public $name = '申请';

    public function handle(Collection $collection)
    {
        try{
            $count = 0;
            $userId = Admin::user()->id;
            foreach ($collection as $model) {
                if(!in_array($model->status, [1,3])){
                    continue;
                }
                if(is_null($model->apply_num))$model->apply_num = 0;
                $model->seller_id = $userId;
                $model->approval_num = $model->apply_num;
                $model->status = 2; //待审批
                $model->approval_date = null;
                $flag = $model->save();
                $flag && $count++;
            }
            return $this->response()->success("成功申请{$count}条记录！")->refresh();
        }catch (\Exception $e){
            report($e);
            return $this->response()->error('系统异常！');
        }
    }
}
